import Base from "./Base"
import turf from "turf"
import turfLength from '@turf/length';
import mapboxgl from "mapbox-gl";

export  default  class DrawLine extends Base{
  constructor(map,{callBack:callBack=null,errorCallBack:errorCallBack=null}={}) {
    super(map,'DrawLine',{callBack:callBack,errorCallBack:errorCallBack});
  }


  start() {
    super.start();
    this.draw = new MapboxDraw({
      displayControlsDefault: false,
      controls: {}}
    );
    this.map.addControl(this.draw);
    this.draw.changeMode(this.draw.modes.DRAW_LINE_STRING);
    this.createdEvent=(e)=>{
      if(this.draw){
        this.update(e);
        }
    };
    this.map.on('draw.create', this.createdEvent);
    console.log("添加监听")
  }
  update(e){
     this.finish(e)
  }


  close() {
    console.log("关闭线开始");
    super.close();
    this.clear();
    this.clearEvent();
    console.log("关闭线结束");
  }

  clear() {
    super.clear();
    this.clearMap();
  }

  clearMap() {
    super.clearMap();
    if(this.draw){
      this.draw.deleteAll();
    }
  }

  clearEvent() {
    super.clearEvent();
    if(this.createdEvent){
      this.map.off('draw.create',this.clearEvent);
      console.log("移除绘制线事件");
      this.createdEvent=null;
    }
    if(this.draw){
      this.map.removeControl(this.draw);
      console.log("移除绘制线控件");
      this.draw=null;
    }
  }
}
